#include "Monstre.h"
#include "../Pion.h"
#include "../Ressources.h"


Monstre::Monstre(SDL_Rect p , SDL_Surface *ecran) : PlateForme(p,Ressources::get_image(MONSTRE4),ecran) {
	estMort = false;
	droite = false;
	haut = false;
	posIni = p;

	tempsPrecedent = ((double)SDL_GetTicks()/100.0) ;

	anim = new Animation(Ressources::get_image(ANIMATION),7,0.5);

	estTouche = false;
}

Monstre::Monstre(SDL_Rect p , SDL_Surface*image , SDL_Surface *ecran) : PlateForme(p,image,ecran) {
    estMort = false;
	droite = false;
	haut = false;
	posIni = p;

	tempsPrecedent = ((double)SDL_GetTicks()/100.0) ;

	anim = new Animation(Ressources::get_image(ANIMATION),7,0.5);

	estTouche = false;
}

Monstre::~Monstre(){
    delete anim;
}

bool Monstre::doitDisparaitre(){
	return Affichable::doitDisparaitre() || estMort;
}

void Monstre::decaler(int nombre){
	PlateForme::decaler(nombre);
	posIni.y += nombre;
}

bool Monstre::reagit(Pion *p){
	p->relancer(60);
	estTouche = true;
	return true;
}

bool Monstre::tuerPion(){
	return !estMort && !estTouche ;
}

void Monstre::estToucheParMisile(){
	estTouche = true;
}



void Monstre::afficher(){
	if(!estTouche){
		double tempsAct = ((double)SDL_GetTicks()/100.0);
		if(estMort)
			position.y ++;
		else if(tempsPrecedent+0.05 < tempsAct) {
			if(droite)
				position.x ++;
			else
				position.x --;

			if(haut)
				position.y --;
			else
				position.y ++;

			if(position.x <= posIni.x-10)
				droite = true;
			if(position.x >= posIni.x+10)
				droite = false;


			if(position.y >= posIni.y + 5)
				haut = true;
			if(position.y <= posIni.y - 5 )
				haut = false;

			tempsPrecedent = tempsAct;
		}
		PlateForme::afficher();
	}else{
		SDL_Surface *img = anim->getSpritAct();
		SDL_Rect pos;
		pos.x = position.x;
		pos.y = position.y;
		SDL_BlitSurface(img,NULL,ecran,&pos);
		SDL_FreeSurface(img);

		if(anim->getNbSpritAfficher() == 7)
			estMort = true;
	}

}

bool Monstre::typeAspire(){
	return false;

}
